<?php

/*
 * This file is the jQuery for send attachment screenlet.
 * This handle the transactions of sending of attachment, externally or internally.
 * 
 * Created by: Peter Agno Jr.
 * Date created: October 21, 2011
 */
?>
        // ********************** START - AJAX FOR SEND ATTACHMENT SCREENLET **********************
            // At the start, ajax the position depending on the shown department
            if ( changePos == true )
                dispPositions("fromBase");
            
            // Start - Change positions depending on the departmentId being set
            jQuery(".deptDrpDwn").live("change",function() {
                buttonId = this.id;
                tempSubstr = buttonId.split('_');
                dispPositions(tempSubstr[1]);
            }); // End - Change positions depending on the departmentId being set
            
            // Start - Ajax the positions
            function dispPositions(scrnPostn) {
                // If, there is no gotten documentId
                //if ( typeof( jQuery("#deptDrpDwn_"+scrnPostn).val() ) != 'undefined' ) {
                
                    jQuery.ajax({
                        type : 'POST',
                        url : 'ajaxRequests/getPositions.php',
                        dataType : 'json',
                        async : false,
                        data: {
                            departmentId : jQuery("#deptDrpDwn_"+scrnPostn).val()
                        },
                        success : function(data){
                            if (data) {
                                // REMOVE THE CHILDREN OF posDrpDwn DROPDOWN
                                jQuery("#posDrpDwn_"+scrnPostn).children().remove();

                                jQuery.each(data, function(index, data) {
                                    jQuery("#posDrpDwn_"+scrnPostn).append("<option value='"+data.positionId+"'>"+data.positionName+"</option>");
                                });
                            }
                        },
                        error : function() {
                            alert("Error in getting the positions.");
                        }
                    });
                
                //} 
                // End If, there is no gotten documentId
            } // End - Ajax the positions
            
            // Start - Change displayed Sent Attachment depending on the receiver type
            jQuery(".receiverType").live("change",function() {
                buttonId = this.id;
                tempSubstr = buttonId.split('_');
                
                if ( jQuery("#"+buttonId).val() == 'External' ) {
                    jQuery(".othersRow_"+tempSubstr[1]).show();
                    jQuery(".deptRow_"+tempSubstr[1]).hide();
                    jQuery(".posRow_"+tempSubstr[1]).hide();
                    
                    jQuery("#sendToIptBx_"+tempSubstr[1]).focus();
                }
                else {
                    jQuery(".deptRow_"+tempSubstr[1]).show();
                    jQuery(".posRow_"+tempSubstr[1]).show();
                    jQuery(".othersRow_"+tempSubstr[1]).hide();
                }
            }); // End - Change displayed Sent Attachment depending on the receiver type
            
            // Start - Ajax the external accounts (search engine)
            jQuery(".sendToIptBx").live("keyup",function() {
                buttonId = this.id;
                tempSubstr = buttonId.split('_');
                
                // Remove all of the data in tableExAcc
                jQuery(".tableExAcc_"+tempSubstr[1]).children().remove();
                
                if ( jQuery("#sendToIptBx_"+tempSubstr[1]).val() != '' && jQuery("#sendToIptBx_"+tempSubstr[1]).val() != ' ' ) {
                    // Start - Ajax the external accounts that is like the input of the user from sendToIptBx
                    jQuery.ajax({
                        type : 'POST',
                        url : 'ajaxRequests/getExternalAccounts.php',
                        dataType : 'json',
                        async : false,
                        data: {
                            inputName : jQuery("#sendToIptBx_"+tempSubstr[1]).val()
                        },
                        success : function(data){
                            if (data) {
                                jQuery.each(data, function(index, data) {
                                    jQuery(".tableExAcc_"+tempSubstr[1]).append("<tr><td style='padding-left : 0px;' class='exAccTd' id='exAccTd_"+data.exAccId+"_"+tempSubstr[1]+"'><span class='exAccTd_"+data.exAccId+"_"+tempSubstr[1]+"'>"+data.exAccName+"</span></td></tr>");
                                });

                                jQuery(".tableExAcc_"+tempSubstr[1]).show();
                            }
                        },
                        error : function() {
                            alert("Error in getting the external accounts.");
                        }
                    });
                    // End - Ajax the external accounts that is like the input of the user from sendToIptBx
                }
                else {
                    jQuery(".tableExAcc_"+tempSubstr[1]).hide();
                }
            }); // End - Ajax the external accounts
            
            // Start - Transfer the name to sendToIptBx when it is click from tableExAcc 
            jQuery('.exAccTd').live("click",function() {
                buttonId = this.id;
                tempSubstr = buttonId.split('_');
                
                jQuery("#sendToIptBx_"+tempSubstr[2]).val( jQuery("."+this.id).html() );
                
                // Remove all of the data in tableExAcc, then hide it
                jQuery(".tableExAcc_"+tempSubstr[2]).children().remove();
                jQuery(".tableExAcc_"+tempSubstr[2]).hide();
            }); // End - Transfer the name to sendToIptBx when it is click from tableExAcc 
            
            // Start - jQuery for sending attachment
            jQuery('.sendToBtn').live("click",function() {
                buttonId = this.id;
                tempSubstr = buttonId.split('_');
                
                // Initialize variables
                varDepartmentId = ""; 
                varPositionId = "";
                varSendToIptBx = "";
                confirmStr = "";
                
                
                // Prepare the variables
                if ( jQuery("#receiverType_"+tempSubstr[1]).val() == "Internal" ) {
                    varDepartmentId = jQuery("#deptDrpDwn_"+tempSubstr[1]).val();
                    varPositionId = jQuery("#posDrpDwn_"+tempSubstr[1]).val();
                    confirmStr = "Are you sure that the " + jQuery("#attachmentId_"+tempSubstr[1]).val() + " will be sent to the " + jQuery("#posDrpDwn_"+tempSubstr[1]+" option:selected").text() + " of " + jQuery("#deptDrpDwn_"+tempSubstr[1]+" option:selected").text() + " department?";
                }
                else {
                    varSendToIptBx = jQuery("#sendToIptBx_"+tempSubstr[1]).val();
                    confirmStr = "Are you sure that the " + jQuery("#attachmentId_"+tempSubstr[1]).val() + " will be sent to " + varSendToIptBx + "?";
                }
                
                if ( varSendToIptBx == "" && jQuery("#receiverType_"+tempSubstr[1]).val() != "Internal" ) {
                // If varSendToIptBx, the input external location of the user is empty, and send button was clicked, then a prompt will appear.
                    alert("Input the external receiver of the attachment.");
                    jQuery("#sendToIptBx_"+tempSubstr[1]).focus();
                }
                else {
                    // Start - Confirm the user if he/she is sure with the sending of attachment
                    chosenCnfrm = confirm(confirmStr);
                    if ( chosenCnfrm == true ) {
                        jQuery.ajax({
                            type : 'POST',
                            url : 'ajaxRequests/addSendAttach.php',
                            dataType : 'json',
                            async : false,
                            data: {
                                receiverType : jQuery("#receiverType_"+tempSubstr[1]).val(),
                                departmentId : varDepartmentId,
                                positionId : varPositionId, 
                                sendToIptBx : varSendToIptBx,
                                attachmentId : jQuery("#attachmentId_"+tempSubstr[1]).val(),
                                scrnPostn : tempSubstr[1]
                            },
                            success : function(data){
                                if ( data.transacFlag == "Success" ) {
                                    jQuery(".successMessages_"+tempSubstr[1]).html(data.msg);
                                    jQuery(".successMessage_"+tempSubstr[1]).show();
                                    jQuery("#sendAttachScrn_"+tempSubstr[1]).hide();
                                    jQuery(".archiveBtnAttch_"+tempSubstr[1]).hide();
                                    window.scrollTo(0, 0);
        //                            jQuery("html, body").animate({ scrollTop: 0 }, "slow");

                                    // Show Acknowledge Sent Attachment Screenlet if the receiver was from external account
                                    if ( data.receiverType == "External" ) {
                                        jQuery("#ackSentAttachScrn_"+tempSubstr[1]).show();
                                        jQuery("#rcvrNameSpan_"+tempSubstr[1]).html(data.exAccName);
                                    }
                                    
                                    // Call also the function that will perform the changes in the attachmentStatus input box
                                    attchId = jQuery("#attachmentId_"+tempSubstr[1]).val();
                                    if ( typeof( jQuery("#attachmentStatus_"+attchId).val() ) != 'undefined' ) {
                                        modifyAttchTransacBtn(attchId, "Sent");
                                    }
                                }
                            },
                            error : function() {
                                alert("Error : Sending attachment was not successful.");
                            }
                        });
                    } // End - Confirm the user if he/she is sure with the sending of attachment
                } // End - Perform the ELSE if the chosen receiver is external and it is not empty, or if it is internal
            }); // End - jQuery for sending attachment
            
            // ********************** END - AJAX FOR SEND ATTACHMENT SCREENLET **********************